package com.二叉树2;




import java.util.LinkedList;
import java.util.Queue;

public class 填充节点的下一个节点右侧节点指针2 {
    static class Solution {
        public Node connect(Node root) {
            if(root == null){
                return null;
            }
            Queue<Node> queue = new LinkedList<>();
            queue.add(root);
            while (!queue.isEmpty()){
                int size = queue.size();
                for(int i  = 0 ; i<size;i++){
                    Node node = queue.poll();
                    if(i<size-1){
                        node.next = queue.peek();
                    }
                    if(node.left != null){
                        queue.add(node.left);
                    }
                    if(node.right != null){
                        queue.add(node.right);
                    }
                }
            }
            return root;
        }

        public static void main(String[] args) {
            Solution solution = new Solution();
            Node node = solution.connect(new Node(new Integer[]{1, 2, 3,4}).next);
        }
    }
}
